<?php

class QaCarousel extends CWidget
{

	public $parentID = 0;
	public $type = 'category';

	public function run()
	{
		if( $this->type == 'model' )
		{
			$data = Controller::$pageData;
			$criteria = new CDbCriteria();
			$criteria->compare( '"pageActive"', 1 );
			$criteria->compare( '"pageType"', 'model' );
			$criteria->compare( '"pageParent"', $data[ 'pageId' ] );
			$criteria->order = '"pageId" desc';
			$count = Page::model()->count( $criteria );
			$pages = new CPagination( $count );
			$pages->pageSize = 16;
			$pages->applyLimit( $criteria );
			$models = Page::model()->findAll( $criteria );
			$search = Page::model()->findAll( "\"pageActive\" = 1 AND \"pageType\" = 'model' AND \"pageParent\" = '{$data[ 'pageId' ]}'" );
			
			foreach( Yii::app()->params[ 'categories' ] as $cat )
			{
				if( !empty( $cat[ 'faqUrl' ] ) )
				{
					$arr1[] = $cat[ 'faqUrl' ];
					$arr2[] = $cat[ 'pageUrl' ];
				}
			}

			foreach( $search as $key => $value )
			{
				$searches[] = array( 'value' => $data[ 'pageTitle' ] . ' ' . $value[ 'pageTitle' ], 'url' => 'qa/' . str_replace( $arr2, $arr1, $value[ 'pageUrl' ] ) );

			}
			$random = ['url'=>'/qa/' . str_replace( $arr2, $arr1, $search[rand(0,count($search)-1)][ 'pageUrl' ] ), 'title' => $data[ 'pageTitle' ] . ' ' . $search[rand(0,count($search)-1)][ 'pageTitle' ] ];
			
			foreach( Yii::app()->params[ 'regions' ] as $key => $value )
			{
				$regions[] = $value[ 'genitive' ];
			}
			$regions = implode( ', ', $regions );

			$this->render( 'qaCorousel', array( 'type' => 'model', 'datas' => $models, 'pages' => $pages, 'searches' => $searches, 'random' => $random ) );
		}
		else
		{
			$c = new CDbCriteria();
			$c->select = '"pageUrl", "pageImages", "pageTitle", "pageType"';
			$c->compare( '"pageParent"', $this->parentID );
			$c->compare( '"pageType"', $this->type );
                        $c->addCondition('"pageUrl" != \'remont-kompyuterov\'');
                        
			$c->order = '"pageSort"';
			$models = Page::model()->findAll( $c );
			$this->render( 'qaCorousel', array( 'models' => $models, 'type' => $this->type ) );
		}
	}

}
